package case5_9;

public class case8 {
    public static void main(String[] args) {
        /*反转：已知一个数租arr={19,28,37,46,50}；用程序实现把数组中的元素值叫唤，
         交换后的数组arr={50,46,37,28,19}，并在控制台输出交换后的数组元素。*/
        int[] arr = {19, 28, 37, 46, 50};
        System.out.println("交换后的数组为：");
        /*System.out.print("arr={");
        for (int i = 0, j = arr.length - 1; i <arr.length; i++,j--) {
            if (j > 0) {
                System.out.print(arr[j] + ",");
            } else {
                System.out.print(arr[j]);
            }
        }
        System.out.println("}");*/
        reverse(arr);
        printArray(arr);
    }
    //方法：反转
    public static void reverse(int [] arr) {
        for (int i = 0, j = arr.length - 1; i <= j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    //方法：遍历数组
    public static void printArray(int[] arr) {
        System.out.print("{");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                System.out.print(arr[i]);
            } else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.println("}");
    }
}
